package com.etang.a2020b1006.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.etang.a2020b1006.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MyView_Temp extends View {

    private int Xpoint = 0;
    private int Ypoint = 0;
    private int Xline = 0;
    private int Yline = 0;
    private int Xheight = 0;
    private int Yheight = 0;
    private int number = 0;
    private List<Float> data = new ArrayList<Float>();
    private List<Integer> Xlable = new ArrayList<Integer>();
    private String[] Ylable = new String[5];

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x1234) {
                MyView_Temp.this.invalidate();
            }
        }
    };

    public MyView_Temp(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {

                    }
                    if (data.size() > 7) {
                        data.remove(0);
                        Xlable.remove(0);
                    }
                    data.add(BaseFragment.temp);
                    Xlable.add(number);
                    number++;
                    handler.sendEmptyMessage(0x1234);
                }
            }
        }).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(1);
        Xpoint = 20;
        Ypoint = canvas.getHeight() - 20;
        Xline = canvas.getWidth() - 40;
        Yline = canvas.getHeight() - 40;
        Yheight = Yline / Ylable.length;
        Xheight = Xline / 7;
        canvas.drawLine(0, 0, canvas.getWidth(), 0, paint);
        canvas.drawLine(0, 0, 0, canvas.getHeight(), paint);
        canvas.drawLine(0, canvas.getHeight(), canvas.getWidth(), canvas.getHeight(), paint);
        canvas.drawLine(canvas.getWidth(), 0, canvas.getWidth(), canvas.getHeight(), paint);
        for (int i = 0; i < Ylable.length; i++) {
            Ylable[i] = String.valueOf(i * 20);
            canvas.drawText(Ylable[i], Xpoint - 20, Ypoint - i * Yheight, paint);
        }
        canvas.drawLine(Xpoint - 10, Ypoint, Xpoint + Xline, Ypoint, paint);
        canvas.drawLine(Xpoint, Ypoint + 10, Xpoint, Ypoint - Yline, paint);
        if (data.size() > 1) {
            for (int i = 1; i < data.size(); i++) {
                if (data.get(i - 1) >= 100 && data.get(i) >= 100) {
                    canvas.drawLine(Xpoint + (i - 1) * Xheight, Ypoint - 5 * Yheight, Xpoint + i * Xheight, Ypoint - 5 * Yheight, paint);
                }
                if (data.get(i - 1) >= 100 && data.get(i) < 100) {
                    canvas.drawLine(Xpoint + (i - 1) * Xheight, Ypoint - 5 * Yheight, Xpoint + i * Xheight, Ypoint - data.get(i) * Yheight / 20, paint);
                }
                if (data.get(i - 1) < 100 && data.get(i) >= 100) {
                    canvas.drawLine(Xpoint + (i - 1) * Xheight, Ypoint - data.get(i - 1) * Yheight / 20, Xpoint + i * Xheight, Ypoint - 5 * Yheight, paint);
                }
                if (data.get(i - 1) < 100 && data.get(i) < 100) {
                    canvas.drawLine(Xpoint + (i - 1) * Xheight, Ypoint - data.get(i - 1) * Yheight / 20, Xpoint + i * Xheight, Ypoint - data.get(i) * Yheight / 20, paint);
                }
            }
            canvas.drawText("温度：" + data.get(data.size() - 1), Xpoint, Ypoint, paint);
        }
    }
}
